package handlers

import (
	"git.hortorgames.com/orange/tang/utils"
	"git.hortorgames.com/serverless/htgserver/apiserver"
	"git.hortorgames.com/serverless/htgserver/consts"
	"time"
)

type Base struct {
	Server *apiserver.Server
}

//func (h *Base) LogReq(ctx *apiserver.Context, req interface{}) {
//	ctx.Logger().WithCallerOffset(1).Info("[Req]", "req", utils.JsonEncodeToStringMust(req))
//}

//func (h *Base) LogReqInternal(ctx *apiserver.InternalContext, req interface{}) {
//	ctx.Logger().WithCallerOffset(1).Info("[ReqInternal]", "req", utils.JsonEncodeToStringMust(req))
//}

func (h *Base) Halt(ctx *apiserver.Context, err *consts.ErrorType) {
	ctx.Logger().WithCallerOffset(1).Error("[Halt]", "code", err.Code, "error", err.Msg, "usedTime", time.Now().Sub(ctx.RequestTime).Seconds())
	h.Server.RespError(ctx, err.Code, err.Msg)
}

func (h *Base) Resp(ctx *apiserver.Context, resp interface{}) {
	ctx.Logger().WithCallerOffset(1).Info("[Resp]", "resp", utils.JsonEncodeToStringMust(resp), "usedTime", time.Now().Sub(ctx.RequestTime).Seconds())
	h.Server.Resp(ctx, resp)
}

func (h *Base) HaltInternal(ctx *apiserver.InternalContext, err *consts.ErrorType) {
	ctx.Logger().WithCallerOffset(1).Error("[HaltInternal]", "code", err.Code, "error", err.Msg, "usedTime", time.Now().Sub(ctx.RequestTime).Seconds())
	h.Server.RespErrorInternal(ctx, err.Code, err.Msg)
}

func (h *Base) RespInternal(ctx *apiserver.InternalContext, resp interface{}) {
	ctx.Logger().WithCallerOffset(1).Info("[RespInternal]", "resp", utils.JsonEncodeToStringMust(resp), "usedTime", time.Now().Sub(ctx.RequestTime).Seconds())
	h.Server.RespInternal(ctx, resp)
}
